#include "jointitem.h"
#include <QJsonArray>
const double makeValueKeepTwoDecimal(const double &dValue, const int &nType)
{
    double dTransValue = 0.00;
    int nTempValue = 0;
    int nValue = 0;
    if (nType == 1)  // %
    {
        nTempValue = dValue * 100000;
        nValue = dValue * 10000;
    }
    else if (nType == 2)  // not %
    {
        nTempValue = dValue * 1000;
        nValue = dValue * 100;
    }

    int nLastPersonValue = nTempValue % 10;
    nLastPersonValue >= 5 ? ++nValue : nValue;
    dTransValue = nValue / 100.00;
    return dTransValue;
}

JointBase::JointBase(const QString &sId,JointType type,QVariant value)
{
    m_strJointID = sId;
    m_eType = type;
    m_vValue = value;
}

JointBase::~JointBase()
{
}
// ========================================================================================
// HeadJoint
// ========================================================================================
HeadJoint::HeadJoint(const QString &sId,JointType type,QVariant value)
    :JointBase(sId, type,value)
{
    headJointStruct = value.value<HeadJointStruct>();
    m_strJointID = sId;
    m_eType = type;
    m_vValue = value;
}

HeadJoint::~HeadJoint()
{

}
/**
     * @introduce   更新数据
     * @author      毛辰飞
     * @data        2024/11/14
**/
void HeadJoint::UpdateData(const QVariant &variant)
{
    headJointStruct=variant.value<HeadJointStruct>();

}
QJsonArray HeadJoint::GetData() const
{
    QJsonArray array;
    array.append(makeValueKeepTwoDecimal(headJointStruct.fPosition,2));
    array.append(makeValueKeepTwoDecimal(headJointStruct.fPositionExpecct,2));
    array.append(makeValueKeepTwoDecimal(headJointStruct.fVocility,2));
    array.append(makeValueKeepTwoDecimal(headJointStruct.fVocilityExpecct,2));
    array.append(makeValueKeepTwoDecimal(headJointStruct.fCurrent,2));
    array.append(makeValueKeepTwoDecimal(headJointStruct.fCurrentExpecct,2));
    return array;

}
// ========================================================================================
// armJoint
// ========================================================================================
ArmJoint::ArmJoint(const QString &sId,JointType type,QVariant value)
    :JointBase(sId, type,value)
{
    armJointStruct = value.value<ArmJointStruct>();
    m_strJointID = sId;
    m_eType = type;
    m_vValue = value;
}

ArmJoint::~ArmJoint()
{

}
void ArmJoint::UpdateData(const QVariant &variant)
{
    armJointStruct=variant.value<ArmJointStruct>();

}
QJsonArray ArmJoint::GetData() const
{
    QJsonArray array;
    array.append(makeValueKeepTwoDecimal(armJointStruct.fPosition,2));
    array.append(makeValueKeepTwoDecimal(armJointStruct.fPositionExpecct,2));
    array.append(makeValueKeepTwoDecimal(armJointStruct.fVocility,2));
    array.append(makeValueKeepTwoDecimal(armJointStruct.fVocilityExpecct,2));
    array.append(makeValueKeepTwoDecimal(armJointStruct.fCurrent,2));
    array.append(makeValueKeepTwoDecimal(armJointStruct.fCurrentExpecct,2));
    return array;

}
// ========================================================================================
// waistjoint
// ========================================================================================
WaistJoint::WaistJoint(const QString &sId,JointType type,QVariant value)
    :JointBase(sId, type,value)
{
    waistJointStruct = value.value<WaistJointStruct>();
    m_strJointID = sId;
    m_eType = type;
    m_vValue = value;
}

WaistJoint::~WaistJoint()
{

}
void WaistJoint::UpdateData(const QVariant &variant)
{
    waistJointStruct=variant.value<WaistJointStruct>();

}
QJsonArray WaistJoint::GetData() const
{
    QJsonArray array;
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fPosition,2));
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fPositionExpecct,2));
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fVocility,2));
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fVocilityExpecct,2));
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fCurrent,2));
    array.append(makeValueKeepTwoDecimal(waistJointStruct.fCurrentExpecct,2));
    return array;

}
// ========================================================================================
// legjoint
// ========================================================================================
LegJoint::LegJoint(const QString &sId,JointType type,QVariant value)
    :JointBase(sId, type,value)
{
    legJointStruct = value.value<LegJointStruct>();
    m_strJointID = sId;
    m_eType = type;
    m_vValue = value;
}

LegJoint::~LegJoint()
{

}
void LegJoint::UpdateData(const QVariant &variant)
{
    legJointStruct=variant.value<LegJointStruct>();

}
QJsonArray LegJoint::GetData() const
{
    QJsonArray array;
    array.append(makeValueKeepTwoDecimal(legJointStruct.fPosition,2));
    array.append(makeValueKeepTwoDecimal(legJointStruct.fPositionExpecct,2));
    array.append(makeValueKeepTwoDecimal(legJointStruct.fVocility,2));
    array.append(makeValueKeepTwoDecimal(legJointStruct.fVocilityExpecct,2));
    array.append(makeValueKeepTwoDecimal(legJointStruct.fCurrent,2));
    array.append(makeValueKeepTwoDecimal(legJointStruct.fCurrentExpecct,2));
    return array;

}
